package com.an.blog.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.an.blog.admin.domain.bo.BlogCategoryBo;
import com.an.blog.admin.domain.vo.BlogCategoryVo;
import com.an.blog.service.IBlogCategoryService;
import com.an.common.annotation.Log;
import com.an.common.annotation.RepeatSubmit;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.core.validate.AddGroup;
import com.an.common.core.validate.EditGroup;
import com.an.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 博客分类
 *
 * @author Ann
 * @date 2023-05-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/blog/category")
public class BlogCategoryController extends BaseController {

    private final IBlogCategoryService blogCategoryService;

    /**
     * 查询博客分类列表
     */
    @SaCheckPermission("blog:category:list")
    @GetMapping("/list")
    public TableDataInfo<BlogCategoryVo> list(BlogCategoryBo bo, PageQuery pageQuery) {
        return blogCategoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询博客分类下拉列表
     *
     * @return {@link TableDataInfo }<{@link BlogCategoryVo }>
     * @author: Ann
     * @createDate: 2023/05/09 18:06:13
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:category:list")
    @GetMapping("/queryCategoryList")
    public R<List<BlogCategoryVo>> queryList() {
        return R.ok(blogCategoryService.queryList(new BlogCategoryBo()));
    }


    /**
     * 获取博客分类详细信息
     *
     * @param categoryId 主键
     */
    @SaCheckPermission("blog:category:query")
    @GetMapping("/{categoryId}")
    public R<BlogCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long categoryId) {
        return R.ok(blogCategoryService.queryById(categoryId));
    }

    /**
     * 新增博客分类
     */
    @SaCheckPermission("blog:category:add")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogCategoryBo bo) {
        return toAjax(blogCategoryService.insertByBo(bo));
    }

    /**
     * 修改博客分类
     */
    @SaCheckPermission("blog:category:edit")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogCategoryBo bo) {
        return toAjax(blogCategoryService.updateByBo(bo));
    }


    /**
     * 状态修改
     *
     * @param bo 业务对象
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/09 16:49:08
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:category:edit")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody BlogCategoryBo bo) {
        return toAjax(blogCategoryService.updateCategoryStatus(bo));
    }

    /**
     * 删除博客分类
     *
     * @param categoryIds 主键串
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/05/11 11:41:46
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:category:remove")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] categoryIds) {
        return toAjax(blogCategoryService.deleteWithValidByIds(Arrays.asList(categoryIds), true));
    }
}
